{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from glob import glob\n",
    "from io import BytesIO\n",
    "import pandas as pd\n",
    "import tweepy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def merge_csvs(files, ignore_name=None):\n",
    "    '''\n",
    "    Appends csvs and returns\n",
    "\n",
    "    Parameters:\n",
    "    ___________\n",
    "\n",
    "    files (List):\n",
    "    List of files to append\n",
    "\n",
    "    Returns:\n",
    "    ________\n",
    "    combined (BytesIO):\n",
    "    BytesIO of the files. Returns None if there is no file\n",
    "    '''\n",
    "    combined = BytesIO()\n",
    "    first = 1\n",
    "\n",
    "    if ignore_name != None:\n",
    "        ignored_files = [file for file in files if ignore_name not in file] \n",
    "    else:\n",
    "        ignored_files = files\n",
    "\n",
    "    if len(ignored_files) >= 1:\n",
    "        for file in ignored_files:\n",
    "            \n",
    "            with open(file, \"rb\") as f:\n",
    "                combined.write(f.read())\n",
    "\n",
    "        combined.seek(0)\n",
    "        \n",
    "        return combined\n",
    "    else:\n",
    "        return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "f = merge_csvs(glob('data/Bot Accounts/*'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"ids.csv\", \"wb\") as out:\n",
    "    out.write(f.read())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('ids.csv', header=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "access_token=\"\"\n",
    "access_token_secret=\"\"\n",
    "consumer_key=\"\"\n",
    "consumer_secret=\"\"\n",
    "\n",
    "auth = tweepy.OAuthHandler(consumer_key, consumer_secret)\n",
    "auth.set_access_token(access_token, access_token_secret)\n",
    "api = tweepy.API(auth)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_usernames(api, ids):\n",
    "    \"\"\" can only do lookup in steps of 100;\n",
    "        so 'ids' should be a list of 100 ids\n",
    "    \"\"\"\n",
    "    t_df = pd.DataFrame(columns={'ID', 'username'})\n",
    "    user_objs = api.lookup_users(user_ids=ids)\n",
    "    \n",
    "    \n",
    "    \n",
    "    for user in user_objs:\n",
    "        t_df = t_df.append(pd.Series({'ID': user.id_str, 'username': user.screen_name}), ignore_index=True)\n",
    "        \n",
    "    return t_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 100\n",
      "100 200\n",
      "200 300\n",
      "300 400\n",
      "400 500\n",
      "500 600\n",
      "600 700\n",
      "700 800\n",
      "800 900\n",
      "900 1000\n",
      "1000 1100\n",
      "1100 1200\n",
      "1200 1300\n",
      "1300 1400\n",
      "1400 1500\n",
      "1500 1600\n",
      "1600 1700\n",
      "1700 1800\n",
      "1800 1900\n",
      "1900 2000\n",
      "2000 2100\n",
      "2100 2200\n",
      "2200 2300\n",
      "2300 2400\n",
      "2400 2500\n",
      "2500 2600\n",
      "2600 2700\n",
      "2700 2800\n",
      "2800 2900\n",
      "2900 3000\n",
      "3000 3100\n",
      "3100 3200\n",
      "3200 3300\n",
      "3300 3400\n",
      "3400 3500\n",
      "3500 3600\n",
      "3600 3700\n",
      "3700 3800\n",
      "3800 3900\n",
      "3900 4000\n",
      "4000 4100\n",
      "4100 4200\n",
      "4200 4300\n",
      "4300 4400\n",
      "4400 4500\n",
      "4500 4600\n",
      "4600 4700\n",
      "4700 4800\n",
      "4800 4900\n",
      "4900 5000\n",
      "5000 5100\n",
      "5100 5200\n",
      "5200 5300\n",
      "5300 5400\n",
      "5400 5500\n",
      "5500 5600\n",
      "5600 5700\n",
      "5700 5800\n",
      "5800 5900\n",
      "5900 6000\n",
      "6000 6100\n",
      "6100 6200\n",
      "6200 6300\n",
      "6300 6400\n",
      "6400 6500\n",
      "6500 6600\n",
      "6600 6700\n",
      "6700 6800\n",
      "6800 6900\n",
      "6900 7000\n",
      "7000 7100\n",
      "7100 7200\n",
      "7200 7300\n",
      "7300 7400\n",
      "7400 7500\n",
      "7500 7600\n",
      "7600 7700\n",
      "7700 7800\n",
      "7800 7900\n",
      "7900 8000\n",
      "8000 8100\n",
      "8100 8200\n",
      "8200 8300\n",
      "8300 8400\n",
      "8400 8500\n",
      "8500 8600\n",
      "8600 8700\n",
      "8700 8800\n",
      "8800 8900\n",
      "8900 9000\n",
      "9000 9100\n",
      "9100 9200\n",
      "9200 9300\n",
      "9300 9400\n",
      "9400 9500\n",
      "9500 9600\n",
      "9600 9700\n",
      "9700 9800\n",
      "9800 9900\n",
      "9900 10000\n",
      "10000 10100\n",
      "10100 10200\n",
      "10200 10300\n",
      "10300 10400\n",
      "10400 10500\n",
      "10500 10600\n",
      "10600 10700\n",
      "10700 10800\n",
      "10800 10900\n",
      "10900 11000\n",
      "11000 11100\n",
      "11100 11200\n",
      "11200 11300\n",
      "11300 11400\n",
      "11400 11500\n",
      "11500 11600\n",
      "11600 11700\n",
      "11700 11800\n",
      "11800 11900\n",
      "11900 12000\n",
      "12000 12100\n",
      "12100 12200\n",
      "12200 12300\n",
      "12300 12400\n",
      "12400 12500\n",
      "12500 12600\n",
      "12600 12700\n",
      "12700 12800\n",
      "12800 12900\n",
      "12900 13000\n",
      "13000 13100\n",
      "13100 13200\n",
      "13200 13300\n",
      "13300 13400\n",
      "13400 13500\n",
      "13500 13600\n",
      "13600 13700\n",
      "13700 13800\n",
      "13800 13900\n",
      "13900 14000\n",
      "14000 14100\n",
      "14100 14200\n",
      "14200 14300\n",
      "14300 14400\n",
      "14400 14500\n",
      "14500 14600\n",
      "14600 14700\n",
      "14700 14800\n",
      "14800 14900\n",
      "14900 15000\n",
      "15000 15100\n",
      "15100 15200\n",
      "15200 15300\n",
      "15300 15400\n",
      "15400 15500\n",
      "15500 15600\n",
      "15600 15700\n",
      "15700 15800\n",
      "15800 15900\n",
      "15900 16000\n",
      "16000 16100\n",
      "16100 16200\n",
      "16200 16300\n",
      "16300 16400\n",
      "16400 16500\n",
      "16500 16600\n",
      "16600 16700\n",
      "16700 16800\n",
      "16800 16900\n",
      "16900 17000\n",
      "17000 17100\n",
      "17100 17200\n",
      "17200 17300\n",
      "17300 17400\n",
      "17400 17500\n",
      "17500 17600\n",
      "17600 17700\n",
      "17700 17800\n",
      "17800 17900\n",
      "17900 18000\n",
      "18000 18100\n",
      "18100 18200\n",
      "18200 18300\n",
      "18300 18400\n",
      "18400 18500\n",
      "18500 18600\n",
      "18600 18700\n",
      "18700 18800\n",
      "18800 18900\n",
      "18900 19000\n",
      "19000 19100\n",
      "19100 19200\n",
      "19200 19300\n",
      "19300 19400\n",
      "19400 19500\n",
      "19500 19600\n",
      "19600 19700\n",
      "19700 19800\n",
      "19800 19900\n",
      "19900 20000\n",
      "20000 20100\n",
      "20100 20200\n",
      "20200 20300\n",
      "20300 20400\n",
      "20400 20500\n",
      "20500 20600\n",
      "20600 20700\n",
      "20700 20800\n",
      "20800 20900\n",
      "20900 21000\n",
      "21000 21100\n",
      "21100 21200\n",
      "21200 21300\n",
      "21300 21400\n",
      "21400 21500\n",
      "21500 21600\n",
      "21600 21700\n",
      "21700 21800\n",
      "21800 21900\n",
      "21900 22000\n",
      "22000 22100\n",
      "22100 22200\n",
      "22200 22300\n",
      "22300 22400\n",
      "22400 22500\n",
      "22500 22600\n",
      "22600 22700\n",
      "22700 22800\n",
      "22800 22900\n",
      "22900 23000\n",
      "23000 23100\n",
      "23100 23200\n",
      "23200 23300\n",
      "23300 23400\n",
      "23400 23500\n",
      "23500 23600\n",
      "23600 23700\n",
      "23700 23800\n",
      "23800 23900\n",
      "23900 24000\n",
      "24000 24100\n",
      "24100 24200\n",
      "24200 24300\n",
      "24300 24400\n",
      "24400 24500\n",
      "24500 24600\n",
      "24600 24700\n",
      "24700 24800\n",
      "24800 24900\n",
      "24900 25000\n",
      "25000 25100\n",
      "25100 25200\n",
      "25200 25300\n",
      "25300 25400\n",
      "25400 25500\n",
      "25500 25600\n",
      "25600 25700\n",
      "25700 25800\n",
      "25800 25900\n",
      "25900 26000\n",
      "26000 26100\n",
      "26100 26200\n",
      "26200 26300\n",
      "26300 26400\n",
      "26400 26500\n",
      "26500 26600\n",
      "26600 26700\n",
      "26700 26800\n",
      "26800 26900\n",
      "26900 27000\n",
      "27000 27100\n",
      "27100 27200\n",
      "27200 27300\n",
      "27300 27400\n",
      "27400 27500\n",
      "27500 27600\n",
      "27600 27700\n",
      "27700 27800\n",
      "27800 27900\n",
      "27900 28000\n",
      "28000 28100\n",
      "28100 28200\n",
      "28200 28300\n",
      "28300 28400\n",
      "28400 28500\n",
      "28500 28600\n",
      "28600 28700\n",
      "28700 28800\n",
      "28800 28900\n",
      "28900 29000\n",
      "29000 29100\n",
      "29100 29200\n",
      "29200 29300\n",
      "29300 29400\n",
      "29400 29500\n",
      "29500 29600\n",
      "29600 29700\n",
      "29700 29800\n",
      "29800 29900\n",
      "29900 30000\n",
      "30000 30100\n",
      "30100 30200\n",
      "30200 30300\n",
      "30300 30400\n",
      "30400 30500\n",
      "30500 30600\n",
      "30600 30700\n",
      "30700 30800\n",
      "30800 30900\n",
      "30900 31000\n",
      "31000 31100\n",
      "31100 31200\n",
      "31200 31300\n",
      "31300 31400\n",
      "31400 31500\n",
      "31500 31600\n",
      "31600 31700\n",
      "31700 31800\n",
      "31800 31900\n",
      "31900 32000\n",
      "32000 32100\n",
      "32100 32200\n",
      "32200 32300\n",
      "32300 32400\n",
      "32400 32500\n",
      "32500 32600\n",
      "32600 32700\n",
      "32700 32800\n",
      "32800 32900\n",
      "32900 33000\n",
      "33000 33100\n",
      "33100 33200\n",
      "33200 33300\n",
      "33300 33400\n",
      "33400 33500\n",
      "33500 33600\n",
      "33600 33700\n",
      "33700 33800\n",
      "33800 33900\n",
      "33900 34000\n",
      "34000 34100\n",
      "34100 34200\n",
      "34200 34300\n",
      "34300 34400\n",
      "34400 34500\n",
      "34500 34600\n",
      "34600 34700\n",
      "34700 34800\n",
      "34800 34900\n",
      "34900 35000\n",
      "35000 35100\n",
      "35100 35200\n",
      "35200 35300\n",
      "35300 35400\n",
      "35400 35500\n",
      "35500 35600\n",
      "35600 35700\n",
      "35700 35800\n",
      "35800 35900\n",
      "35900 36000\n",
      "36000 36100\n",
      "36100 36200\n",
      "36200 36300\n",
      "36300 36400\n",
      "36400 36500\n",
      "36500 36600\n",
      "36600 36700\n",
      "36700 36800\n",
      "36800 36900\n",
      "36900 37000\n",
      "37000 37100\n",
      "37100 37200\n",
      "37200 37300\n",
      "37300 37400\n",
      "37400 37500\n",
      "37500 37600\n",
      "37600 37700\n",
      "37700 37800\n",
      "37800 37900\n",
      "37900 38000\n",
      "38000 38100\n",
      "38100 38200\n",
      "38200 38300\n",
      "38300 38400\n",
      "38400 38500\n",
      "38500 38600\n",
      "38600 38700\n",
      "38700 38800\n",
      "38800 38900\n",
      "38900 39000\n",
      "39000 39100\n",
      "39100 39200\n",
      "39200 39300\n",
      "39300 39400\n",
      "39400 39500\n",
      "39500 39600\n",
      "39600 39700\n",
      "39700 39800\n",
      "39800 39900\n",
      "39900 40000\n",
      "40000 40100\n",
      "40100 40200\n",
      "40200 40300\n",
      "40300 40400\n",
      "40400 40500\n",
      "40500 40600\n",
      "40600 40700\n",
      "40700 40800\n",
      "40800 40900\n",
      "40900 41000\n",
      "41000 41100\n",
      "41100 41200\n",
      "41200 41300\n",
      "41300 41400\n",
      "41400 41500\n",
      "41500 41600\n",
      "41600 41700\n",
      "41700 41800\n",
      "41800 41900\n",
      "41900 42000\n",
      "42000 42100\n",
      "42100 42200\n",
      "42200 42300\n",
      "42300 42400\n",
      "42400 42500\n",
      "42500 42600\n",
      "42600 42700\n",
      "42700 42800\n",
      "42800 42900\n",
      "42900 43000\n",
      "43000 43100\n",
      "43100 43200\n",
      "43200 43300\n",
      "43300 43400\n",
      "43400 43500\n",
      "43500 43600\n",
      "43600 43700\n",
      "43700 43800\n",
      "43800 43900\n",
      "43900 44000\n",
      "44000 44100\n",
      "44100 44200\n",
      "44200 44300\n",
      "44300 44400\n",
      "44400 44500\n",
      "44500 44600\n",
      "44600 44700\n",
      "44700 44800\n",
      "44800 44900\n",
      "44900 45000\n",
      "45000 45100\n",
      "45100 45200\n",
      "45200 45300\n",
      "45300 45400\n",
      "45400 45500\n",
      "45500 45600\n",
      "45600 45700\n",
      "45700 45800\n",
      "45800 45900\n",
      "45900 46000\n",
      "46000 46100\n",
      "46100 46200\n",
      "46200 46300\n",
      "46300 46400\n",
      "46400 46500\n",
      "46500 46600\n",
      "46600 46700\n",
      "46700 46800\n",
      "46800 46900\n",
      "46900 47000\n",
      "47000 47100\n"
     ]
    }
   ],
   "source": [
    "full_df = pd.DataFrame(columns={'ID', 'username'})\n",
    "\n",
    "for i in range(100, df.shape[0], 100):\n",
    "    curr_ids = list(df[i-100:i][0].values)\n",
    "    t_df = get_usernames(api, curr_ids)\n",
    "    \n",
    "    full_df = full_df.append(t_df)\n",
    "    \n",
    "    print(\"{} {}\".format(i-100, i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [],
   "source": [
    "full_df.to_csv('data/bot_id_username.csv', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
